#include "importdialog.h"
#include "ui_importdialog.h"

ImportDialog::ImportDialog(QWidget *parent, CModData *Baza, QDir currentDir) :
    QDialog(parent),
    ui(new Ui::ImportDialog)
{
    ui->setupUi(this);
    this->currentDir = currentDir;
    this->Baza = Baza;
    ui->tableWidget->setRowCount(0);
    ui->tableWidget->setColumnCount(3);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    QStringList labels;
    labels << tr("Directory") << tr("File Name") << tr("Size");
    ui->tableWidget->setHorizontalHeaderLabels(labels);
    ui->tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->hide();
    ui->tableWidget->setShowGrid(false);

}

ImportDialog::~ImportDialog()
{
    delete ui;
}

void ImportDialog::on_pushButton_2_clicked() // OK
{
    CSkrypty Import;
    QString kod;
    int fili = 0; //licznik plików
    qDebug() << ui->le_nazawa_projektu->text();
    kod = ui->le_kodowanie->text();
    nazwa_projektu = ui->le_nazawa_projektu->text();


        QVariant v;
        QVariant v2;
        v.setValue(openfile.size());
        //qDebug() << openfile.size() << "Pliki";
        for (QStringList::Iterator it = openfile.begin(); it != openfile.end(); ++it){

        QFile file;
        file.setFileName(*it);
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        Import.parsuj(file, Baza, kod, 0);
        file.close();
        fili ++;
        }

        v.setValue(Import.glob_Ldialog);
        v2.setValue(fili);
        qDebug() << tr("Znaleziono %1 dialogów w %2 plikach.").arg(v.toString(),v2.toString());
        log = tr("Znaleziono %1 dialogów w %2 plikach.").arg(v.toString(),v2.toString());
        dialli = Import.glob_Ldialog;

        Baza = new CModData[Import.glob_Ldialog];

        ui->progressBar->setMaximum(fili);

        fili = 0;
        Import.glob_Ldialog = 0;
        int plk_Ldialog = 0;
        for (QStringList::Iterator it = openfile.begin(); it != openfile.end(); ++it){

        QFileInfo fi;
        fi.setFile(*it);
        qDebug() << fi.fileName();
        qDebug() << currentDir.relativeFilePath(fi.path());

        plk_Ldialog = Import.glob_Ldialog; //ilość dialogów w poprzednim pliku.
        fili ++;
        ui->progressBar->setValue(fili);
        ui->label->setText(currentDir.relativeFilePath(fi.filePath()));
        v.setValue(fili);
        QFile file;
        file.setFileName(*it);
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        Import.parsuj(file, Baza, kod, 1);
        qDebug() << tr("Ilość wierszy w pliku: %1").arg(Import.linia);
        file.close();


        qDebug() << tr("Pierwszy wpis jest w %1 linii %2 pliku: '%3'.").arg(QString::number(Baza[plk_Ldialog].nr_wiersza), v.toString(), Baza[plk_Ldialog].tresc);
        qDebug() << tr("Ostatni wpis jest w %1 linii %2 pliku: '%3'.").arg(QString::number(Baza[(Import.glob_Ldialog)-1].nr_wiersza), v.toString(), Baza[(Import.glob_Ldialog)-1].tresc);
        }
        accept();
}

void ImportDialog::on_pushButton_clicked() // Dodoawanie plików.
{
    openfile = QFileDialog::getOpenFileNames(this,
         tr("Open Dedelus Scripts"), currentDir.path(), tr("Scripts (*.d)"));
    int fili = 0; //licznik plików
    //int li =0;
    QVariant v;
    QVariant v2;
    v.setValue(openfile.size());
    qDebug() << openfile.size() << "Pliki";

    //ui->tableWidget->setRowCount(openfile.size());
    for (QStringList::Iterator it = openfile.begin(); it != openfile.end(); ++it)
    {

    QFileInfo fi;
    fi.setFile(*it);
    qDebug() << fi.fileName();
    qDebug() << currentDir.relativeFilePath(fi.path());
    ui->tableWidget->insertRow(fili);
    QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(currentDir.relativeFilePath(fi.path())));
    ui->tableWidget->setItem(fili, 0, newItem);
    QTableWidgetItem *newItem2 = new QTableWidgetItem(tr("%1").arg(fi.fileName()));
    ui->tableWidget->setItem(fili, 1, newItem2);
    QTableWidgetItem *newItem3 = new QTableWidgetItem(tr("%1").arg(fi.size()));
    ui->tableWidget->setItem(fili, 2, newItem3);
    fili++;
    }
    ui->toolBox->setCurrentIndex(1);
}

void ImportDialog::file_parser()
{


}

void ImportDialog::on_pb_dod_kat_clicked() // Dodawanie katalogu.
{
    //term->append(tr("Open Directory"));

    QString directory_path = QFileDialog::getExistingDirectory(this, tr("Select your .d scripts directory"), currentDir.path());
    QDirIterator directory_walker(directory_path, QDir::Files | QDir::NoSymLinks, QDirIterator::Subdirectories);
    while(directory_walker.hasNext())
    {

        // then we tell our directory_walker object to explicitly take next element until the loop finishes
        directory_walker.next();
        // I want to list just d files!
        if(directory_walker.fileInfo().completeSuffix() == "d")
        {
            openfile << directory_walker.filePath();
        }
    }


    int fili = 0; //licznik plików
    //int li =0;
    QVariant v;
    QVariant v2;
    v.setValue(openfile.size());
    qDebug() << openfile.size() << "Pliki";

    //ui->tableWidget->setRowCount(openfile.size());
    for (QStringList::Iterator it = openfile.begin(); it != openfile.end(); ++it)
    {

    QFileInfo fi;
    fi.setFile(*it);
    qDebug() << fi.fileName();
    qDebug() << currentDir.relativeFilePath(fi.path());
    ui->tableWidget->insertRow(fili);
    QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(currentDir.relativeFilePath(fi.path())));
    ui->tableWidget->setItem(fili, 0, newItem);
    QTableWidgetItem *newItem2 = new QTableWidgetItem(tr("%1").arg(fi.fileName()));
    ui->tableWidget->setItem(fili, 1, newItem2);
    QTableWidgetItem *newItem3 = new QTableWidgetItem(tr("%1").arg(fi.size()));
    ui->tableWidget->setItem(fili, 2, newItem3);
    fili++;
    }
    ui->toolBox->setCurrentIndex(1);

}
